package com.robot.base.util

import android.app.Activity
import android.content.Context
import android.net.Uri
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.engine.CompressFileEngine
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.utils.DateUtils
import com.robot.base.util.image.GlideEngine
import com.robot.base.util.image.Luban
import com.robot.base.util.image.OnNewCompressListener
import java.io.File

object ChoiceImgHelper {

    fun ChoiceImg(
        activity: Activity, selectNum: Int,
        resultFunc: (result: List<LocalMedia?>) -> Unit,
        cancelFunc: () -> Unit
    ) {


        //TODO 相册调整
        PictureSelector.create(activity)
            .openGallery(SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine())
            .setMaxSelectNum(selectNum)
            .setCompressEngine(ImageFileCompressEngine())
//            .selectionMode(PictureConfig.MULTIPLE)
//            .maxSelectNum(6)
//            .isCompleteSelectRelativeTop
//            .minimumCompressSize(100)
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onCancel() {
                    cancelFunc.invoke()
                }

                override fun onResult(result: ArrayList<LocalMedia?>?) {
                    resultFunc(result!!)
                }
            })
    }

    fun ChoiceOpenCamera(
        activity: Activity,
        resultFunc: (result: List<LocalMedia?>) -> Unit?,
        cancelFunc: () -> Unit?
    ) {
        //TODO 相机调整
        PictureSelector.create(activity)
            .openCamera(SelectMimeType.ofImage())
            .setCompressEngine(ImageFileCompressEngine())
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onCancel() {
                    cancelFunc
                }

                override fun onResult(result: ArrayList<LocalMedia?>?) {
                    resultFunc(result!!)
                }

            })
    }
//    fun ChoiceImg(
//        activity: Fragment,
//        resultFunc: (result: List<LocalMedia?>) -> Unit,
//        cancelFunc: () -> Unit?
//    ) {
//        PictureSelector.create(activity)
//            .openGallery(PictureMimeType.ofImage())
//            .imageEngine(GlideEngine.createGlideEngine())
//            .isCompress(true)
//            .selectionMode(PictureConfig.SINGLE)
//            .minimumCompressSize(100)
//            .forResult(object : OnResultCallbackListener<LocalMedia?> {
//                override fun onResult(result: List<LocalMedia?>) {
//                    resultFunc(result)
//                }
//
//                override fun onCancel() {
//                    cancelFunc
//                }
//            })
//    }

    /**
     * 自定义压缩
     */
    private class ImageFileCompressEngine : CompressFileEngine {
        override fun onStartCompress(
            context: Context,
            source: ArrayList<Uri>,
            call: OnKeyValueResultCallbackListener
        ) {
            Luban.with(context).load(source).ignoreBy(100)
                .setRenameListener { filePath ->
                    val indexOf = filePath.lastIndexOf(".")
                    val postfix = if (indexOf != -1) filePath.substring(indexOf) else ".jpg"
                    DateUtils.getCreateFileName("CMP_") + postfix
                }.setCompressListener(object : OnNewCompressListener {
                    override fun onStart() {}
                    override fun onSuccess(source: String?, compressFile: File) {
                        if (call != null) {
                            call.onCallback(source, compressFile.absolutePath)
                        }
                    }

                    override fun onError(source: String?, e: Throwable?) {
                        if (call != null) {
                            call.onCallback(source, null)
                        }
                    }
                }).launch()
        }
    }
}